﻿
using System.Collections.Generic;
using System.Data;
using System;
namespace ZHDJ.MobileInterface
{
    public partial class CenterPortalLogin : System.Web.UI.Page
    {  
        protected void Page_Load(object sender, System.EventArgs e)
        {
            string result = "{result:0}";
            string pwd = string.Empty;
            string userID = Request.QueryString["uname"] ?? "";
            string passwd = Request.QueryString["passwd"] ?? "";
            string loginID = string.Empty;
            bool flag = true;

            if (string.IsNullOrEmpty(userID) || string.IsNullOrEmpty(passwd))
            {
                result = "{result:1,msg:登录账号,密码不能为空}";
                flag = false;
            }


            if (CommonUtils.IsDisableUsers(userID))
            {
                result = "{result:1,msg:用户状态有问题，请联系管理员}";
                flag = false;
            }
            if (flag)
            {
                loginID = InterfaceHelper.GetLoginID(userID);
                if (string.IsNullOrEmpty(loginID))
                { 
                    result = "{result:1,msg:登录账号不存在}";
                    flag = false;
                } 
            }


            if (flag)
            {
                pwd = CommonUtils.GetMD5(passwd.ToLower());
                flag = InterfaceHelper.PasswordIsCorrect(loginID, pwd);

                if (!flag)
                { 
                    result = "{result:1,msg:登录账号，密码输入不正确}";
                    flag = false;
                }
            }

            //检查登录账号是否开通
            if (flag)
            {
                if (!InterfaceHelper.LoginIDEnable(loginID))
                { 
                    result = "{result:1,msg:您的账号还没启用，请联系系统管理员}";
                    flag = false;
                }
            }

            ResponseHelper.WriteResponseMessage(Response, result);

            //string result = "{code:0}";
            //string pwd = string.Empty;
            //string userID = Request.QueryString["uid"]??"";
            //string time = Request.QueryString["time"] ?? "";
            //string sn = Request.QueryString["sn"] ?? "";
            //int len = "YYYYMMDDHHmmss".Length;
            //bool flag = true;

            //if (string.IsNullOrEmpty(userID) || time.Length != len) 
            //    result = "{code:0}";  

            //if (flag)
            //{
            //    //获取用户密码
            //    DataSet ds = InterfaceHelper.GetUseInfo(userID);
            //    if(ds != null && ds.Tables[0].Rows.Count == 1)
            //    {
            //        pwd = ds.Tables[0].Rows[0]["Password"].ToString(); 
            //        if (sn.ToLower() == CommonUtils.GetMD5(userID + ServiceSettings.CenterLoginKey + pwd + time))
            //        {
            //            result = "{code:1,displayname:'" + ds.Tables[0].Rows[0]["UserName"].ToString() + "'}";
            //        } 
            //    } 
            //} 

            //ResponseHelper.WriteResponseMessage(Response, result);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static string GetMD5(string strSource)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSource, "MD5").ToLower(); 
        }
    } 
}